import { useAxios, UseAxiosOptions, StrictUseAxiosReturn } from '@vueuse/integrations/useAxios';
import axiosInstance from './request';
import type { AxiosRequestConfig, Method, AxiosResponse } from 'axios';

interface CustomAxiosRequestConfig extends AxiosRequestConfig {
  headers?: {
    isToken?: boolean; // 是否携带token
    isEncrypt?: boolean; // 请求参数是否加密
    [K: string]: any;
  };
}

export function useRequest<T = any, R = AxiosResponse<T>, D = any>(
  url: string,
  method: Method = 'GET',
  config: Omit<CustomAxiosRequestConfig, 'method' | 'url'> = {},
  options: UseAxiosOptions = { immediate: false }
): StrictUseAxiosReturn<T, R, D> & Promise<StrictUseAxiosReturn<T, R, D>> {
  return useAxios(
    url,
    {
      ...config,
      method
    },
    axiosInstance,
    options
  );
}

export default useRequest;
